#pragma once
#include <DirectXMath.h>
#include "Constants.h"


// forward declaration
class PolynomialCalculator;

class Polynomial
{
	int mDegree;
	DirectX::XMVECTOR mA[kMaxDegree + 1][kMaxDegree + 1];

	friend class PolynomialCalculator;
public:
	int GetDegree() const;

	// for given x=(-1...+1) calculates the set of derivatives from Legendre polynomials
	void GetValuesOfBaseFunctions(float x, int bufferLength, float* buffer) const;

	// result of the two-dimensional function, using the pre-calculated base functions values
	COLORREF CalculateColor(int buffersLength, const float* bufferX, const float* bufferY) const;

	// buffer outRGB should be three elements long. Output is not restricted to 0..255
	DirectX::XMVECTOR CalculateColorComponents(float x, float y) const;
	
	// allocators are needed for DirectMath objects inside
	void *__cdecl operator new(size_t count){ return _aligned_malloc(count, 16); };
	void __cdecl operator delete(void* p) { _aligned_free(p); };
};

// ------- auxiliary class -------
class PolynomialCalculator
{
	int mDegree;
	int mSide;
	float mDeltaF[kMaxDegree + 1][kMaxBitmapSide + 1];

public:
	PolynomialCalculator();
	void SetDegreeAndSide(int degree, int side);
	int GetDegree() const;
	void ClearPolynomial(Polynomial& outPolynomial);
	void UpdateThePolynomialWithPixel(Polynomial& outPolynomial, int x, int y, COLORREF color);

};
